﻿
function Pl_image(obj)
{
  this.x = 0;
  this.y = 0;
  this.w = 0;
  this.h = 0;
  this.sx = 0;
  this.sy = 0;
  this.sw = 0;
  this.sh = 0;
  this.was = [];
  this.was_r = [];
  this.angle = 0;
  this.vangle = 0;
  this.animation_iters = 0;
  this.animation_frame = 0;
  this.animation_frame_counter = 1;
  this.animation_name = '';
  this.animations = [];
  this.actions = [];
  this.object = obj;
  this.object.layer.ready = 0;
  this.object.ready = 0;
  this.ready = 0;
  this.time;
  this.img = new Image();
  this.img.parent = this;
  this.img.onload =  function()
  {
          this.parent.ready = 1;
          this.parent.object.tryReady();
  }
  this.gx = function()
  {
          return Math.round(this.x) ;//+ this.object.layer.x;
  }
  this.gy = function()
  {
          return Math.round(this.y) ;//+ this.object.layer.y;
  }
  /**
   * @name - название анимации
   * @row - Строка спрайта
   * @frames - Количество кадров в анимации
   * @speed - показы каждого кадра
   */
  this.addAnimation = function(name,row,frames,speed)
  {
          if(!speed) var speed = [];
          this.animations[name] = {'row':row,'frames':frames,'speed':speed};
  }
  /**
   * @name - название запускаемой анимации
   * @iter - количество повторов
   */
  this.runAnimation = function(name,iter)
  {
    if(this.animations[name])
    {
      this.animation_name = name;
      this.was['sy'] = this.sy;
      this.was['sx'] = this.sx;
      this.sy = (this.animations[name]['row'] -1) * this.sh;
      this.sx = 0;
      this.animation_frame = 1;
      this.animation_iters = iter;
    }
    return this;
  }
  this.stopAnimation = function()
  {
    this.animation_iters = 0;
  }
  this.addNextAction = function(action)
  {// ПРимер object.addNextAction({'animation':'run2','iter':4});
    this.actions.push(action);
  }

  this.update = function()
  {
          this.object.update();
  }
  this.checkUpdate = function()
  {
          if(this.animation_frame > 0) {
                  this.update();
                  return true;
          }
          if(this.animation_iters != 0){
                  this.update();
                  return true;
          }
          if(this.a_iter > 0)
          {
                  this.update();
                  return true;
          }
          return false;
  }

  this.draw = function()
  {

          if(!this.w && !this.h)
          {
                  this.object.layer.getCtx().drawImage(this.img, this.gx(), this.gy());
                 /* this.object.layer.getCtx().drawImage({
                    image: this.img,
                    from: [this.gx(), this.gy()],
                    optimize: true
                  });
                  //*/
          }
          else if(!this.sw && !this.sh && !this.sx && !this.sy)
          {
                  this.object.layer.getCtx().drawImage(this.img, this.gx(), this.gy(),this.w,this.h);
          }
          else
          {
                  if(this.animation_frame > 0)
                  {
                          //console.error(this.animations[this.animation_name]['frames'],this.animation_frame);
                          if(this.animation_frame != 1 && this.animation_frame <= this.animations[this.animation_name]['frames'])
                          {
                                  this.sx = this.sw * (this.animation_frame - 1);
                          }
                          else if(this.animation_frame >= this.animations[this.animation_name]['frames'])
                          {// Дошли до последнего кадра
                                  this.sy = this.was['sy'];
                                  this.sx = this.was['sx'];
                                  this.animation_frame = -1;
                                  this.was = [];
                                  // Презапускаем анимацию
                                  if(this.animation_iters > 1) this.runAnimation(this.animation_name,this.animation_iters-1)
                                  else if(this.animation_iters < 0) this.runAnimation(this.animation_name,-1);
                                  else if(this.actions.length > 0){
                                    var action = this.actions.shift();
                                    console.info(action);
                                    if(action.animation)
                                    {
                                      this.runAnimation(action.animation,action.iter);  
                                    }
                                    if(action.move)
                                    {
                                      this.object.moveTo(action.move.x,action.move.y,action.move.time)
                                    }
                                  }
                          }
                          if(!this.animations[this.animation_name]['speed'][this.animation_frame-1])
                          {
                                  this.animations[this.animation_name]['speed'][this.animation_frame-1] = 1;
                          }
                          if(this.animations[this.animation_name]['speed'][this.animation_frame-1] <= this.animation_frame_counter)
                          {
                                  this.animation_frame++;
                                  this.animation_frame_counter=1;
                          }
                          else{
                                  this.animation_frame_counter++;
                          }
                          //console.info(this.sy,this.sx);
                  }
                  //console.info(this.sy);
                  this.object.layer.getCtx().drawImage(this.img,this.sx,this.sy,this.sw,this.sh, this.gx(), this.gy(),this.w,this.h);
          }
  }
}